package cn.gaozhong.user.service.impl;

import cn.gaozhong.basic.constart.BaseConstant;
import cn.gaozhong.basic.dto.RegisterDto;
import cn.gaozhong.basic.exception.CustomException;
import cn.gaozhong.basic.util.MD5Utils;
import cn.gaozhong.basic.util.StrUtils;
import cn.gaozhong.user.domain.LoginInfo;
import cn.gaozhong.user.domain.User;
import cn.gaozhong.user.mapper.LogininfoMapper;
import cn.gaozhong.user.mapper.UserMapper;
import cn.gaozhong.user.service.IUserService;
import cn.gaozhong.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaozhong
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 手机号注册
     * @param dto
     */
    @Override
    public void registerPhone(RegisterDto dto) {
        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        //空校验
        if (StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(smsCode)
                ||StringUtils.isEmpty(password)
                ||StringUtils.isEmpty(passwordRepeat)){
            throw new CustomException("参数不能为空！！！");
        }
        //判断两次密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new CustomException("密码不一致请重新输入");
        }
        //获取短信验证码
        String key = BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + phone;
        Object codeTmp = redisTemplate.opsForValue().get(key);
        //判断短信验证码是否错误或者过期
        if (codeTmp!=null){
            if (!smsCode.equalsIgnoreCase(codeTmp.toString().split(":")[0])){
                throw new CustomException("验证码填写错误请核对");
            }
            //保存数据将dto数据保存到user
            User user = dto2User(dto);
            //保存数据将user数据保存到logininfo
            LoginInfo logininfo = user2LoginInfo(user);
            //logininfo做新增
            logininfoMapper.save(logininfo);
            //将logininfo的id赋值给user表的LogininfoId
            user.setLogininfoId(logininfo.getId());
            //user做新增
            userMapper.save(user);
        }else {
            throw new CustomException("验证码过期  请重新获取");
        }
    }

    /**
     * 邮件注册
     * @param dto
     */
    @Override
    public void registerEmail(RegisterDto dto) {
        String email = dto.getEmail();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        if (StringUtils.isEmpty(email)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(passwordRepeat)){
            throw new CustomException("参数不能为空！！！");
        }
        //判断两次密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new CustomException("密码不一致请重新输入");
        }
        //保存数据将dto数据保存到user
        User user = dto2User(dto);
        //保存数据将user数据保存到logininfo
        LoginInfo logininfo = user2LoginInfo(user);
        //logininfo做新增
        logininfoMapper.save(logininfo);
        //将logininfo的id赋值给user表的LogininfoId
        user.setLogininfoId(logininfo.getId());
        //user做新增
        userMapper.save(user);
    }

    private LoginInfo user2LoginInfo(User user) {
        LoginInfo logininfo = new LoginInfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    private User dto2User(RegisterDto dto) {
        User user = new User();
        if (dto.getPhone()==null){
            user.setUsername(dto.getEmail());
            user.setEmail(dto.getEmail());
            String salt = StrUtils.getComplexRandomString(32);
            String password = MD5Utils.encrypByMd5(dto.getPassword() + salt);
            user.setSalt(salt);
            user.setPassword(password);
        }else{
            user.setUsername(dto.getPhone());
            user.setPhone(dto.getPhone());
            String salt = StrUtils.getComplexRandomString(32);
            String password = MD5Utils.encrypByMd5(dto.getPassword() + salt);
            user.setSalt(salt);
            user.setPassword(password);
        }
        return user;
    }


}
